import { Component, OnInit } from '@angular/core';
import { FetchService } from '../../../../core/net/fetch.service';
import * as moment from 'moment';
import { FormBuilder, FormGroup } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
    selector: 'app-dd-section',
    templateUrl: './dd-section.component.html',
    styleUrls: ['./dd-section.component.scss']
})
export class DdSectionComponent implements OnInit {
    selectedDate = new Date();
    form: FormGroup;
    // contentTitle = '断面引水量及水情分析';
    contentTitle = '水情分析';

    constructor(private fetchService: FetchService,
                private fb: FormBuilder,
                private message: NzMessageService) {
        this.form = this.fb.group({
            id: [null],
            // diversion1: [],
            // planDiversion1: [],
            // diversion2: [],
            // planDiversion2: [],
            waterInfo: [],
            dutyManange: [],
            dutyWorker: []
        });
    }

    ngOnInit(): void {
        this.queryInfo();
    }

    queryInfo(): void {
        this.contentTitle = `${moment(this.selectedDate).year()}年${moment(this.selectedDate).month() + 1}月${moment(this.selectedDate).date()}日水情分析`;
        this.fetchService.post('schedule/yjqDiversion2B/list', {
            pageSize: 1000,
            ctCreatetime: moment(this.selectedDate).format('YYYY-MM-DD')
        }).subscribe(res => {
            this.form.reset(res['list'][0] || {});
        });
    }

    submitForm(): void {
        const url = this.form.get('id').value ? 'schedule/yjqDiversion2B/edit' : 'schedule/yjqDiversion2B/add';
        this.fetchService.post(url, {
            ctCreatetime: moment(this.selectedDate).format('YYYY-MM-DD'),
            ...this.form.value
        }).subscribe(res => {
            this.message.info('提交成功！');
        });
    }

    resetForm(): void {
        this.queryInfo();
        this.message.info('重置成功！');
    }
}
